<!--
 * @Description: 权限管理 增/改页面
 * @Autor: zhangzhanhua
 * @Date: 2021-04-12 09:46:35
 * @LastEditors: zhangzhanhua
 * @LastEditTime: 2021-11-25 16:40:11
-->
<template>
    <div class="addOrEdit">
        <div class="manage">
            <el-form label-position="right" class="elFormWeb" :model="manageData" :rules="manageDataRule" ref="manageData" v-loading='loading'>
                <el-form-item label="权限编码" prop="authorityCode" label-width="120px">
                    <el-input type="text" size='mini' v-model="manageData.authorityCode"></el-input>
                </el-form-item>
                <el-form-item label="权限名称" prop="authorityName" label-width="120px">
                    <el-input type="text" size='mini' v-model="manageData.authorityName"></el-input>
                </el-form-item>
                <el-form-item label="备注" label-width="120px">
                    <el-input type="textarea" size='mini' v-model="manageData.remark"></el-input>
                </el-form-item>
            </el-form>
            <div class="userBtn">
                <el-button size="small" type="primary" @click="saveAdd" :disabled='loading'>保 存</el-button>
            </div>
        </div>
    </div>

</template>

<script>
export default {
    components: {

    },
    props: {
        //选中项id
        parentId: {
            type: String | Number,
        },
        //状态
        type: {
            type: String,
            default: 'add'
        },
        // 关闭页面
        close: {
            type: Function
        },
    },
    data() {
        return {
            loading: false,//编辑时防止快速点击
            manageData: {
                authorityCode: '', //权限编码
                authorityName: '', //权限名称
                remark: '', //备注
            },
            //校验规则
            manageDataRule: {
                authorityCode: [
                    {
                        required: true,
                        message: '权限编码不能为空',

                    }
                ],
                authorityName: [
                    {
                        required: true,
                        message: '权限名称不能为空',
                    }
                ],
            },
        }
    },
    mounted() {
        this.getRowData(); //编辑时获取当前行数据
    },
    methods: {
        /**
         * @description:编辑时获取该行数据
         * @param {*}
         * @return {*}
         * @author: zhangzhanhua
         */
        getRowData() {
            if (this.type == 'edit') {
                this.loading = true
                this.$http(`/${this.$ServiceName}/api/external/exauthority/${this.$version}/getexauthoritybyid`, {
                    params: {
                        id: this.parentId
                    }
                }).then(res => {
                    if (res.data.success) {
                        this.loading = false
                        let data = {
                            authorityCode: res.data.data.authorityCode, //权限编码
                            authorityName: res.data.data.authorityName, //权限名称
                            remark: res.data.data.remark, //备注
                        }
                        this.manageData = data
                    } else {
                        this.$message.error(res.data.msg)
                    }
                })
            }
        },
        /**
         * @description: 保存新增
         * @param {*}
         * @return {*}
         * @author: zhangzhanhua
         */
        saveAdd() {
            this.$refs.manageData.validate(valid => {
                if (valid) {
                    let params = JSON.parse(JSON.stringify(this.manageData))
                    //新增
                    if (this.type == 'add') {
                        this.$http.post(`/${this.$ServiceName}/api/external/exauthority/${this.$version}/insert`, params).then(res => {
                            if (res.data.success) {
                                this.$message.success(res.data.msg)
                                this.close()
                            } else {
                                this.$message.error(res.data.msg)
                            }
                        })
                    } else { //编辑
                        params.id = this.parentId
                        this.$http.put(`/${this.$ServiceName}/api/external/exauthority/${this.$version}/updatebyid`, params).then(res => {
                            if (res.data.success) {
                                this.$message.success(res.data.msg)
                                this.close()
                            } else {
                                this.$message.error(res.data.msg)
                            }
                        })
                    }
                }
            })

        }
    },
}
</script>

<style lang="less" scoped>
.addOrEdit {
    .userBtn {
        z-index: 100;
        text-align: right;
        padding-right: 20px;
        border-top: 1px solid #c8cfde;
        background-color: #f5f7fa;
        position: absolute;
        bottom: 0px;
        left: 0;
        right: 0;
        height: 50px;
        line-height: 50px;
    }
}
.manage {
    /deep/ .drawer .el-input__inner {
        width: 100% !important;
    }
    .elFormWeb {
        // height: 654px;
        padding: 10px 30px 50px 10px;
        overflow: auto;
    }
}
</style>
